package com.sptpc.company.service.impl;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sptpc.company.common.ServerResponse;
import com.sptpc.company.dao.ArticleMapper;
import com.sptpc.company.pojo.Article;
import com.sptpc.company.service.IArticleService;
import com.sptpc.company.common.Const;
import com.sptpc.company.pojo.User;


@Service("iArticleService")
public class ArticleServiceImpl implements IArticleService{

	@Autowired
	private ArticleMapper articleMapper;
	
	
	public ServerResponse seek(int id,HttpSession session) {
		// TODO Auto-generated method stub
		User user = (User) session.getAttribute(Const.CURRENT_USER);//得到当前的用户
		if(user == null) {
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		int count =articleMapper.checkaid(id);
		if(count == 0) {
			return ServerResponse.createByErrorMessage("商品不存在");
		}
		return ServerResponse.createBySuccess(articleMapper.Aseek(id,session));
	}

	
	public ServerResponse<String> insertseek(String aname,String content,String price,HttpSession session) {
		// TODO Auto-generated method stub
		User user = (User) session.getAttribute(Const.CURRENT_USER);//得到当前的用户
		if(user == null) {
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		Article ainsert = new Article();
		ainsert.setAname(aname);
		ainsert.setContent(content);
		ainsert.setPrice(price);
		int count = articleMapper.insert(ainsert);
		return ServerResponse.createBySuccess("插入商品成功");
	}


	public ServerResponse delet(int id, HttpSession session) {
		// TODO Auto-generated method stub
		User user = (User) session.getAttribute(Const.CURRENT_USER);//得到当前的用户
		if(user == null) {
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		int count1 =articleMapper.checkaid(id);
		if(count1 == 0) {
			return ServerResponse.createByErrorMessage("商品不存在");
		}
		Article count = articleMapper.shanchu(id,session);
		return ServerResponse.createBySuccess("删除商品成功");
	}




	public ServerResponse update(Integer id,String aname, String content, HttpSession session) {
		// TODO Auto-generated method stub
		User user = (User) session.getAttribute(Const.CURRENT_USER);//得到当前的用户
		if(user == null) {
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		Article aupdate = new Article();
		aupdate.setId(id);
		aupdate.setAname(aname);
		aupdate.setContent(content);
		int count1 =articleMapper.checkaid(id);
		if(count1 == 0) {
			return ServerResponse.createByErrorMessage("商品不存在");
		}
		int count = articleMapper.update(aupdate);
		return ServerResponse.createBySuccess("更新商品成功",aupdate);
	}

}
